/*
 * @Author: i@douxuefeng.cn
 * @Date: 2022-04-12 12:45:48
 * @LastEditTime: 2022-04-12 13:50:25
 * @LastEditors: i@douxuefeng.cn
 * @Description:
 * @FilePath: \go-wechat\pay\h5\h5.go
 */
package h5

import (
	"gitee.com/douxuefeng/go-wechat/pay/config"
	"gitee.com/douxuefeng/go-wechat/pay/tools"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
	pay_h5 "github.com/wechatpay-apiv3/wechatpay-go/services/payments/h5"
)

type h5 struct {
	*config.PayConfig
}

func NewH5(cfg *config.PayConfig) *h5 {
	return &h5{cfg}
}

func (p *h5) PrepayOrder(prepay Prepay) (*pay_h5.PrepayResponse, *core.APIResult, error) {
	client, ctx, err := tools.GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := pay_h5.H5ApiService{Client: client}
	resp, result, err := svc.Prepay(ctx, pay_h5.PrepayRequest{
		Appid:       core.String(p.Appid),
		Mchid:       core.String(p.MchId),
		Description: core.String(prepay.Description),
		OutTradeNo:  core.String(prepay.OutTradeNo),
		Attach:      core.String(prepay.Attach),
		NotifyUrl:   core.String(p.Notify),
		Amount: &pay_h5.Amount{
			Total: core.Int64(prepay.Amount),
		},
		SceneInfo: &pay_h5.SceneInfo{
			PayerClientIp: core.String(prepay.ClientIp),
			H5Info: &pay_h5.H5Info{
				Type: core.String(prepay.H5Info),
			},
		},
	})
	return resp, result, err
}
func (p *h5) QueryOrderByOutTradeNo(outTradeNo string) (*payments.Transaction, *core.APIResult, error) {
	client, ctx, err := tools.GetClient(p.PayConfig)
	if err != nil {
		return nil, nil, err
	}
	svc := pay_h5.H5ApiService{Client: client}
	resp, result, err := svc.QueryOrderByOutTradeNo(ctx, pay_h5.QueryOrderByOutTradeNoRequest{
		OutTradeNo: core.String(outTradeNo),
		Mchid:      core.String(p.MchId),
	})
	return resp, result, err
}
